<template name="buttonAre">
    <div class="display flow buttonAre">
        <el-button type="primary" icon="el-icon-plus" @click="newGoodsClick" v-if="$store.state.system_type == 0">新增商品</el-button>
        <el-button type="warning" icon="el-icon-star-on" @click="recommendClick" v-if="$store.state.system_type == 1">申请推荐</el-button>
        <el-button type="danger" icon="el-icon-delete" @click="deleteClick" v-if="$store.state.system_type == 0">删除</el-button>
    </div>
</template>

<script>
export default {
    name: 'buttonAre',
    props: ['tableSelect'],
    methods: {
        newGoodsClick(){
            this.$router.push({
                path: '/newGoods',
                query: {
                    type: 1,
                    list: ''
                }
            })
        },
        recommendClick(){
            if(this.tableSelect.length != 0){
                this.recommend(this.tableSelect)
            }else{
                this.$message('请选择商品！');
            }
        },
        async recommend(list){
            for(var i in list){
                if(list[i].tj != 1){
                    let res = await this.$Http.recommendGoods({id: list[i].id})
                    console.log(res)
                    if(res.code == 1){
                        this.$message({
                            type: 'success',
                            message: list[i].product_name + '申请推荐成功!'
                        });
                    }
                }else{
                    this.$message({
                        type: 'warning',
                        message: list[i].product_name + '此商品已为推荐商品!'
                    });
                }
            }
            this.$emit('refreshTableFun')
        },
        deleteClick(){
            if(this.tableSelect.length != 0){
                console.log(this.tableSelect)
                this.$confirm('此操作将永久删除您所选的商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.operationGoods(this.tableSelect)
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }else{
                this.$message('请选择商品！');
            }
        },
        async operationGoods(list){
            for(var i in list){
                let res = await this.$Http.operationGoods({
                    type: 3,
                    id: list[i].id
                })
                console.log(res)
                if(res.code == 1){
                    this.$message({
                        type: 'success',
                        message: list[i].product_name + '删除成功!'
                    });
                }
            }
            this.$emit('refreshTableFun')
      },
    }
}
</script>

<style>
    .buttonAre{
        width: 100%;
        justify-content: flex-start;
        margin: 20px 0;
    }
    .el-button{
        margin-right: 10px
    }
</style>
